
#include "Sprite.h"




//Metodo construtor da classe Sprite - OK
Sprite::Sprite(string arquivo)
{
	surface = NULL;

	this->load(arquivo);
	this->surfaceRotoZoom = NULL;
	this->angle = 0;
	this->scaleX = 1.0;
	this->scaleY = 1.0;
}



//Metodo destrutor da classe Sprite - OK
Sprite::~Sprite()
{
	if (this->surface != NULL)
		SDL_FreeSurface(this->surface);
}



//Carrega uma surface chamando a loadImage da SDLBase - OK
void Sprite::load(string arquivo)
{
	if (surface != NULL)
		SDL_FreeSurface(surface);

	surface = SDLBase::loadImage(arquivo);

	clipRect.x = 0;
	clipRect.y = 0;
	clipRect.w = surface->w;
	clipRect.h = surface->h;
}



//Define os valores do retangulo de clipping - OK
void Sprite::clip(int x, int y, int w, int h)
{
	if ((clipRect.x != x || clipRect.y != y || clipRect.w != w || clipRect.h != h))
	{
		clipRect.x = x;
		clipRect.y = y;
		clipRect.w = w;
		clipRect.h = h;

		if (surfaceRotoZoom != NULL)
			surfaceRotoZoom = rotoZoom(angle, scaleX, scaleY, true);
	}
}



//Retorna o retangulo de clipping - OK
SDL_Rect Sprite::getClip()
{
	return clipRect;
}



//Renderiza uma surface chamando a renderSurface da SDLBase - OK
void Sprite::render(int x, int y)
{

    dst.x= x;
    dst.y= y;
    dst.w= 0;
    dst.h= 0;

    if (surfaceRotoZoom == NULL)
    {
    	if((clipRect.x < 0)||(clipRect.y < 0))
    	{
    		aux.x = clipRect.x;
    		aux.y = clipRect.y;

    		SDLBase::renderSurface(surface,&clipRect,&dst);

    		clipRect.x = aux.x;
    		clipRect.y = aux.y;
    	}
    	else
    	{
    		SDLBase::renderSurface(surface,&clipRect,&dst);
    	}
    }
    else
    {
    	if ((clipRect.w/2 - surfaceRotoZoom->w/2) > 0)
    		dst.x += clipRect.w/2 - surfaceRotoZoom->w/2;
    	else
    	{
    		if((clipRect.w/2 - surfaceRotoZoom->w/2) < 0)
    			dst.x += clipRect.w/2 - surfaceRotoZoom->w/2;
    	}
    	if ((clipRect.h/2 - surfaceRotoZoom->h/2) > 0)
    	    dst.y += clipRect.h/2 - surfaceRotoZoom->h/2;
    	else
    	{
    		if((clipRect.h/2 - surfaceRotoZoom->h/2) < 0)
    			dst.y += clipRect.h/2 - surfaceRotoZoom->h/2;
    	}

    	aux.w = surfaceRotoZoom->w;
    	aux.h = surfaceRotoZoom->h;
    	aux.x = 0;
    	aux.y = 0;

    	SDLBase::renderSurface(surfaceRotoZoom,&aux,&dst);

    }
}



//Retorna a width atual da surface - OK
int Sprite::getWidth()
{
	if (surfaceRotoZoom)
		return surfaceRotoZoom->w;
	else
		return surface->w;
}



//Retorna a height atual da surface - OK
int Sprite::getHeight()
{
	if (surfaceRotoZoom)
		return surfaceRotoZoom->h;
	else
		return surface->h;
}

//OK
void Sprite::restore()
{
	delete(surfaceRotoZoom);
}

//OK
SDL_Surface* Sprite::rotoZoom(float angle, float scalex = 1.0f, float scaley = 1.0f, bool force = false)
{
	SDL_Rect aux;

	if ((angle != angle) || (scaleX != scaleX) || (scaleY != scaleY) || (force))
	{
		if (surfaceRotoZoom != NULL)
			SDL_FreeSurface(surfaceRotoZoom);

		this->angle = angle;
		this->scaleX = scalex;
		this->scaleY = scaley;

		if ((clipRect.x != 0) || (clipRect.y != 0) || (clipRect.w != surface->w) || (clipRect.h != surface->h))
		{
				aux.x = clipRect.x;
				aux.y = clipRect.y;
				aux.w = clipRect.w;
				aux.h = clipRect.h;
				surfaceRotoZoom = SDLBase::clip(surface, &aux);
				surfaceRotoZoom = SDLBase::rotoZoom(surfaceRotoZoom,angle,scaleX,scaleY);
		}
		else
		{
			clipRect.w = surface->w*scaleX;
			clipRect.h = surface->h*scaleY;
			surfaceRotoZoom = SDLBase::rotoZoom(surface,angle,scaleX,scaleY);
		}
	}
	return surfaceRotoZoom;
}

